<template>
  <div class="bodyBox">
    <FrameStyle>
      <!-- 面包屑 -->
      <Breadcrumb :breadName="`轮播图管理`" slot="breadcrumb" />
      <!-- 搜索框 -->
      <el-input slot="search">
        <el-button class="el-icon-search" slot="append"></el-button>
      </el-input>
      <!-- 添加按钮 -->
      <el-button type="primary" slot="addBtn" @click="addDiaLogVisible = true"
        >添加轮播图</el-button
      >
      <!-- 表格列表 -->
      <el-table :data="tableData" stripe slot="tableList" height="370px">
        <el-table-column label="图片名称" prop="name"></el-table-column>
        <el-table-column prop="picture" label="图片">
          <template slot-scope="scope">
            <img :src="scope.row.picture" height="70px" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="openChangeDialog(scope.row)" size="mini">修改</el-button>
            <el-button type="danger" size="mini" @click="delCarousel(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <Pagination
        slot="pageSlot"
        :pageNo="pageNum"
        :pageSize="pageSize"
        :totalRows="totalRows"
        @pageChange="pageChange"
      />
    </FrameStyle>
    <el-dialog :visible.sync="addDiaLogVisible">
      <el-form :model="addForm">
        <el-form-item label="图片名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="轮播图片">
          <UploadImg :type="`CAROUSEL`" @getImgUrl="getImgUrl" />
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="addCarousel" type="primary">确认</el-button>
        <el-button>重置</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="changeDiaLogVisible">
      <el-form :model="changeForm" ref="ruleForm">
        <el-form-item label="图片名称" prop="name">
          <el-input v-model="changeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="轮播图片" prop="picture">
          <UploadImg :type="`CAROUSEL`" @getImgUrl="getImgUrl" />
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="changeCarousel" type="primary">确认</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqLunBoTuList,
  reqAddData,
  reqDelData,
  reqChangeData,
} from "../../../api";
export default {
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      totalRows: 1,
      imgUrl: "",
      addForm: {
        name: "",
        picture: "",
      },
      changeForm: {},
      addDiaLogVisible: false,
      changeDiaLogVisible: false,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取轮播图数据
    async getData() {
      let { pageNum, pageSize } = this;
      let result = await reqLunBoTuList({ pageNum, pageSize });
      if (result.code != 200) return console.error(result.err);
      result.data.list.forEach((item) => {
        item.createTime = this.$format(item.createTime);
      });
      this.tableData = result.data.list;
      this.totalRows = result.data.total;
    },
    //添加轮播图数据
    async addCarousel() {
      this.addForm.picture = this.imgUrl;
      let result = await reqAddData("/carousel/add", this.addForm);
      if (result.code != 200) return this.$message.error("添加失败");
      this.getData();
      this.$message({
        type: "success",
        message: "添加成功",
      });
      this.addDiaLogVisible = false;
    },
    //删除轮播图
    async delCarousel(id) {
      let result = await reqDelData("/carousel/delete", id);
      if (result.code != 200) return this.$message.error("删除失败");
      this.getData();
      this.$message({
        type: "success",
        message: "删除成功",
      });
    },
    //打开修改弹出层
    openChangeDialog(row) {
      this.changeDiaLogVisible = true;
      this.$nextTick(() => {
        this.changeForm = row;
      });
    },
    //重置表单
    resetForm(formName){
      this.$refs[formName].resetFields()
    },
    //修改轮播图
    async changeCarousel() {
      this.imgUrl != "" ? (this.changeForm.picture = this.imgUrl) : false;
      let result = await reqChangeData("/carousel/update", this.changeForm);
      if (result.code != 200) return this.$message.error("修改失败");
      this.getData();
      this.$message({
        type: "success",
        message: "修改成功",
      });
      this.changeDiaLogVisible = false;
    },
    //接收上传图片路径
    getImgUrl(url) {
      this.imgUrl = url;
    },
    //分页操作
    pageChange(page) {
      this.pageNum = page;
      this.getData();
    },
  },
};
</script>